package com.example.custom_view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.custom_view.DensityUtils;
import com.example.custom_view.R;

/**
 * @author yaotianxue
 * @description
 * @date 2024/9/28
 */
public class MyView extends View {
    public static final String TAG = "MyView";
    private Paint paint;//画笔
    private int color = Color.BLUE;//画笔颜色
    private float radius = 30;//半径
    public MyView(Context context) {
        super(context);
        initPaint();
    }
    //xml布局
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获得自定义属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        radius = typedArray.getInteger(R.styleable.MyView_radius,30);
        color = typedArray.getColor(R.styleable.MyView_color,Color.BLUE);
        typedArray.recycle();//释放自定义属性资源
        initPaint();
    }
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }
    /**
     * 初始化画笔
     */
    private void initPaint(){
        paint = new Paint();
        paint.setColor(color);
        paint.setAntiAlias(true);//抗锯齿
        paint.setStrokeWidth(DensityUtils.dpToPx(getContext(), 5));//粗细
        paint.setStyle(Paint.Style.STROKE);//样式：FILL实心  STROKE空心
        paint.setDither(true);//防抖动
    }
    /**
     * 绘制
     * @param canvas  画布
     */
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, DensityUtils.dpToPx(getContext(), radius), paint);
    }

    /**
     * 测量
     * （1）测量规格32位(MeasureSpec) = 测量模式2位(MeasureMode) + 测量大小30位(MeasureSize)
     * （2）测量模式3种
     *      a。EXACTLY：精确尺寸。match_parent 或精确数值。
     *      b。AT_MOST：最大尺寸。wrap_content。
     *      c。UNSPECIFIED：未指定尺寸。listview\recyclerview
     *
     *
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //宽度
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if(widthMode == MeasureSpec.EXACTLY){//不处理
            Log.d(TAG, "onMeasure: 精确模式");
        }else if(widthMode == MeasureSpec.AT_MOST){//wrap_content 处理
            Log.d(TAG, "onMeasure: 最大值");
            widthSize =(int) DensityUtils.dpToPx(getContext(), (radius+5)*2);
        }else{
            Log.d(TAG, "onMeasure: 未指定");
        }

        //高度
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if(heightMode == MeasureSpec.EXACTLY){//不处理
            Log.d(TAG, "onMeasure: 精确模式");
        }else if(heightMode == MeasureSpec.AT_MOST){//wrap_content 处理
            Log.d(TAG, "onMeasure: 最大值");
            heightSize =(int) DensityUtils.dpToPx(getContext(), (radius+5)*2);
        }else{
            Log.d(TAG, "onMeasure: 未指定");
        }
        //设置宽高
        setMeasuredDimension(widthSize,heightSize);




    }
}
